
var BasicTableEditor = (function() {
	function BasicTableEditor(congViecBanGiaos, target, callback) {
		var _this = this;
		// declare an array to contain value of each row
		_this.trVals = [];
		// looking for table parent
		var parent = $("#" + target).closest("table");
		_this.tableObject = parent;
		// If table is found
		if (parent.is("table")) {
			var row = null;

			//initialize data for table
			if(congViecBanGiaos.length > 0) {
				var congViecBanGiao = null;

				for(var i = 0; i < congViecBanGiaos.length; i++) {
					congViecBanGiao = congViecBanGiaos[i];
					row = callback.addOriginalRows($("<tr>"), congViecBanGiao, function(trVal) {
						_this.trVals.push(trVal);
					});
					row.insertAfter(parent.find("tr:last"));
				}
			}
			
			// declare click event for target
			$("#" + target).click(function() {
				row = callback.addEvent($("<tr>"), function(trVal) {
					_this.trVals.push(trVal);
				});
				// insert editable row at the first index
				row.insertAfter(parent.find("tr:first"));
			});
		}
		;
		// If table is not found, do nothing here
	};
	
	/*
	 * 
	 */
	BasicTableEditor.prototype.getValues = function() {
		var _this = this;

		var values = [];
		var value = null;
		for ( var i = 0; i < _this.trVals.length; i++) {
			value = _this.trVals[i]();

			if (value != null) {
				values.push(value);
			};
		}
		return values;
	};
	/*
	 * 
	 */
	return BasicTableEditor;
})();

var YeuCauTrichLuc = (function() {
	function YeuCauTrichLuc() {
	}

	YeuCauTrichLuc.prototype.initComponents = function(args) {
		$("#" + args.ngaySinhDoiTuongTrichLuc).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy"
		});

		$("#" + args.ngayDeNghiTraTrichLuc).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy",
			minDate: new Date()
		});
		$("#" + args.ngayCapSoCMND).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy"
		});
		$("#" + args.ngayLapTrichLuc).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy"
		});
		$("#" + args.ngayCapSoHoChieu).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy"
			
		});
	};

	return YeuCauTrichLuc;
})();

var ThemTienSu = (function() {
	function ThemTienSu() {
	}

	ThemTienSu.prototype.initComponents = function(args) {
		$("#" + args.ngayToaAn).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy",
		});


	
	};

	return ThemTienSu;
})();

var XoaAnTich = (function() {
	function XoaAnTich() {
	}

	XoaAnTich.prototype.initComponents = function(args) {
		$("#" + args.ngayYeuCauXoaAnTich).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy",
			onClose: function( selectedDate ) {
				 $("#" + args.ngayKyDuyetXoaAnTich).datepicker( "option", "minDate", selectedDate );
				 }
		});

		$("#" + args.ngayKyDuyetXoaAnTich).datepicker({
			showOn : "both",
			buttonImage : args.calendarIcon,
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy",
			onClose: function( selectedDate ) {
				 $("#" + args.ngayYeuCauXoaAnTich).datepicker( "option", "maxDate", selectedDate );
				 }
		});
	};

	return XoaAnTich;
})();

function loadQuanHuyen(selectedTinhId, quanHuyenIdToBeChanged,
		phuongXaIdToBeChanged, loadQuanHuyenURL, oldQuanHuyenId) {
	var tinhId = $("#" + selectedTinhId).val();
	var quanHuyenIdNameSpace = '#' + quanHuyenIdToBeChanged;
	var phuongXaIdNameSpace = '#' + phuongXaIdToBeChanged;

	loadQuanHuyenURL = loadQuanHuyenURL.replace(/%7B0%7D/g, function() {
		return tinhId;
	});

	jQuery.ajax({
		url : loadQuanHuyenURL,
		dataType : 'json',
		type : "POST",
		cache : false,
		timeout : 10000,
		async : false,
		success : function(data) {

			$(quanHuyenIdNameSpace+' option :not(:first)').remove();
			$(phuongXaIdNameSpace + ' option :not(:first)').remove();

			if (data.length > 0) {
				$.each(data, function(index, item) {
					$(quanHuyenIdNameSpace).append($("<option/>", {
						value : item.quanHuyenId,
						text : item.tenQuanHuyen
					}));
				});

				$(quanHuyenIdNameSpace).find("option").each(function() {
					if ($(this).val() == oldQuanHuyenId) {
						$(this).attr("selected", "selected");
					}
				});
			}
		},
		error : function(xhr, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function loadPhuongXa(selectedQuanHuyenId, phuongXaIdToBeChanged,
		loadPhuongXaURL, oldPhuongXaId) {
	var quanHuyenId = $("#" + selectedQuanHuyenId).val();
	var phuongXaIdNameSpace = '#' + phuongXaIdToBeChanged;

	loadPhuongXaURL = loadPhuongXaURL.replace(/%7B(\d+)%7D/g, function() {
		return quanHuyenId;
	});
	jQuery.ajax({
		url : loadPhuongXaURL,
		dataType : 'json',
		type : "POST",
		cache : false,
		timeout : 10000,
		async : false,
		success : function(data) {
			$(phuongXaIdNameSpace+' option :not(:first)').remove();
			if (data.length > 0) {
				$.each(data, function(index, item) {
					$(phuongXaIdNameSpace).append($("<option/>", {
						value : item.phuongXaId,
						text : item.tenPhuongXa
					}));
				});

				$(phuongXaIdNameSpace).find("option").each(function() {
					if ($(this).val() == oldPhuongXaId) {
						$(this).attr("selected", "selected");
					}
				});
			}
		},
		error : function(xhr, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}


function confirmMsg() {
	var co = '<liferay-ui:message key="co" />';
 	var khong = '<liferay-ui:message key="khong" />';

	showPopupConfirm('<liferay-ui:message key="ban-co-muon-luu-thong-tin" />',[co, khong], function(index) {
	    if (index != 0){
		    return ;
		}
	});
}
function getSelectedItem(selectList){
	var selectedTienSu = [];
	for (i in selectList){
		var isChecked =  selectList.eq(i).prop('checked');
		var item = selectList.eq(i).val();
		if (isChecked && item && item !=''){
			selectedTienSu.push(item);
		}
	}
	return selectedTienSu;
}
(function($){

	
	
    $.fn.extend({

        checktree: function(){
            $(this)
                .addClass('checktree-root')
                .on('change', 'input[type="checkbox"]', function(e){
                    e.stopPropagation();
                    e.preventDefault();

                    checkParents($(this));
                    checkChildren($(this));
                })
            ;

            var checkParents = function (c)
            {
                var parentLi = c.parents('ul:eq(0)').parents('li:eq(0)');

                if (parentLi.length)
                {
                    var siblingsChecked = parseInt($('input[type="checkbox"]:checked', c.parents('ul:eq(0)')).length),
                        rootCheckbox = parentLi.find('input[type="checkbox"]:eq(0)')
                    ;

                    if (c.is(':checked')){
                        rootCheckbox.prop('checked', true)
                        checkButtonCheckAll();
                    }
                    else if (siblingsChecked === 0){
                        rootCheckbox.prop('checked', false);
                        checkButtonCheckAll();
                    }

                    checkParents(rootCheckbox);
                }
            }

            var checkChildren = function (c)
            {
                var childLi = $('ul li input[type="checkbox"]', c.parents('li:eq(0)'));

                if (childLi.length){
                    childLi.prop('checked', c.is(':checked'));
                    checkButtonCheckAll();
                }
            }
        }

    });
})(jQuery);
